/**
 * Created by lhd on 2018-04-12.  自定义指令
 */
var url="";
routerApp.directive("pagination",['$http','localStorageService',function($http,localStorageService) {
    return{
        scope:{
            refresh : '&',
            count:'=',
            pagesize:'=',
            page:'='
        },
        restrict:'EAC',
        replace: true,
        templateUrl:'/view/index/page.html',
        controller:function($scope,$element,$attrs,localStorageService){
			
            $scope.totalPage=1;//总页数
            //首页
            $scope.firstPage=function(){
                $scope.page=1;
            }
            //下一页
            $scope.nextPage= function () {
                var page=parseInt($scope.page)+1;
                if($scope.totalPage>=page){
                    $scope.page=page;
                }else{
                    //toaster.pop('error', "消息", "已是最后一页");
                    alert("已是最后一页！")
                }
            }
            //上一页
            $scope.upPage= function () {
                var page=$scope.page-1;
                if(page>0){
                    $scope.page=page;
                }else{
                    alert("已是第一页");
                }
            }
            $scope.endPage=function(){
                $scope.page=$scope.totalPage;
            }

            $scope.getList=function(){
                if(!$scope.totalPage){}else{
                    var size=10;


                    var min=parseInt($scope.page)-1;
                    var max=parseInt($scope.page)+9;
                    if($scope.totalPage>10){
                        if(max>$scope.totalPage){
                            min=parseInt($scope.totalPage)-9;
                            max=$scope.totalPage;
                        } else{
                            if(min==0){
                                var min=$scope.page;
                                var max=parseInt($scope.page)+10;
                            }
                        }
                    }else{
                        size=$scope.totalPage;
                        min=1;
                    }
                    $scope.arrayObj = new Array([size]);//创建一个数组并指定长度，注意不是上限，是长度

                    for (var i=0;i<size; i++)
                    {
                        $scope.arrayObj[i]={key:"dd",value:parseInt(i)+parseInt(min)};
                    }
                }
            }
            $scope.$watch('page', function(newVal, oldVal) {
                if($scope.totalPage>=newVal&&newVal>=1){
                    $scope.page=newVal;

                    $scope.refresh();
                }else{
                    $scope.page=oldVal;
                   alert("超出最大可查询值");
                }
               // console.log( $scope.page);
                $scope.getList();

            });
            //单个按钮跳转
            $scope.setPage=function(page){
                $scope.page=page;
            }
            $scope.$watch('pagesize', function(newVal, oldVal) {
                $scope.refresh();
            });
            $scope.$watch('count', function(newVal, oldVal) {
                var z =$scope.count%$scope.pagesize;
                if(z==0){
                    if(Math.floor($scope.count/$scope.pagesize)==0){
                        $scope.totalPage=1;
                    }else{

                        $scope.totalPage=Math.floor($scope.count/$scope.pagesize);
                    }
                }else{
                    $scope.totalPage=Math.floor($scope.count/$scope.pagesize)+1;
                }
                $scope.getList();
            });
        }

    };
}]);

/*2018-4-17 17:26:28 lhd 日历模板*/
routerApp.directive('calendar',['localStorageService','$http','$location',function(localStorageService,$http,$location) {
    return {
        scope:{
            index:'=',
            selected:'=',
            terraPolicy:'&',
            rows:'=',
            sx:'='
        },
        restrict: 'EAC',
        transclude:true,
        templateUrl:'../index/Calendar.html',
        controller: function($scope,localStorageService,$http) {


            if($scope.index===$scope.selected){
                $scope.showme=true;
            }else{
                $scope.showme=false;
            }

            $scope.$watch('selected', function(newVal, oldVal) {

                if($scope.index===$scope.selected){
                    $scope.showme=true;
                    $scope.refreshDay();
                }else{
                    $scope.showme=false;
                }
            });
            $scope.$watch('sx', function(newVal, oldVal) {

                if($scope.index===$scope.selected){
                   if($scope.sx==1) {
                    $scope.refreshDay();}
                }else{
                    $scope.showme=false;
                }
            });

            $scope.save=function(ptPrice){
                var month=($scope.cmonth+1)>9?($scope.cmonth+1)+"":"0"+($scope.cmonth+1);
                var  ordertimeBegin=get_unix_time($scope.year+"-"+month+"-"+$scope.rday+" 12:00:00");
                var  ordertimeEnd=ordertimeBegin;

                if(!ptPrice){
                    alert("必须修改后的价格！")
                    return;
                }

                if(parseInt(ptPrice)<0){
                    alert("金额必须大于0")
                    return;
                }

                var data={
                    hid: $scope.rows.hid,
                    rid:$scope.rows.rid,
                    startTime:ordertimeBegin,
                    endTime:ordertimeEnd,
                    iszmyz:0,
                    prPrice:ptPrice
                }
                $http.post(url + '',data)
                    .success(function(resp) {
                        if(resp.status == 'success'){
                            $scope.refreshDay();
                            alert("设置成功！");
                            //ngDialog.closeAll();
                        } else {
                            alert(resp.errorMsg);
                        }

                    })
                    .error(function(resp) {

                    });
            }

            $scope.weeks=[ '星期日' ,'星期一', '星期二', '星期三', '星期四', '星期五', '星期六' ];
            $scope.months=[ '一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ];
                $scope.today = new Date();
            $scope.month =  $scope.months[$scope.today.getMonth()];
            $scope.cmonth = $scope.today.getMonth();
            $scope.bmonth = $scope.today.getMonth();
            $scope.cDate=$scope.today.getDate();
            $scope.year =  $scope.today.getFullYear();

            $scope.cday=0;//当前日期
            $scope.mouseove=function(day){
                $scope.cday=day;
            }
            $scope.mouseleave=function(){
                $scope.cday=0;
            }
            //刷新页面显示天数
            $scope.refreshDay=function(){
//获取天数：
                var  firstDay = new Date($scope.year,$scope.cmonth, 1 );
                var  max = new Date($scope.year,$scope.cmonth+1,0).getDate();
                //console.log(max);
                // day of the week
                var  startingDay = firstDay.getDay();
                $scope.startdays=[];
                $scope.enddays=[];
                $scope.days=[];
                for(var i=0;i<startingDay;i++){
                    $scope.startdays.push(35+i+1);
                }
                //获取当前第一个
                for(var i=1;i<=max;i++){
//              	if(parseInt(i) < 10) {
//              		var a='0'+i
//              		$scope.days.push({day:parseInt(a),minPrice:"...."});
//              	}else{
                		$scope.days.push({day:i,minPrice:"....",status:""});
//              	}
                    
                }
                var sy=35-max-startingDay;
                for(var i=1;i<=sy;i++){
                    $scope.enddays.push(35+i+startingDay);
                }
                var Data = {
                	hid:localStorageService.get('hid'),
                	year:$scope.year,
                	month:$scope.cmonth+1,
                	totalday:max,
                	rid:$scope.rows.rid
                };
                $http.get('/api/index/dzdeviant',"").success(function(resp){
                    if(resp.success){
                    	for(var a in resp.data){
                    		var date = new Date(resp.data[a].time); 
                      		var d=date.getDate();
                      		var sta=resp.data[a].status;
                        	for(var index in $scope.days){
                            	if(index>=0){								
									if($scope.days[index].day==d){
										$scope.days[index].status=sta;
									}														
								}
                        	}
                       }
                    }else{
                    	
                    }
					
                }).error(function(list) {
                    //处理错误
                });

            }
            $scope.refreshDay();
            //上一月
            $scope.customPrev=function(){
                var cm= $scope.cmonth-1;
                if(cm>=0){
                    $scope.cmonth=cm;
                }else{
                    $scope.cmonth=11;
                    $scope.year=$scope.year-1;
                }
                $scope.month =  $scope.months[$scope.cmonth];
                $scope.refreshDay();
            }
            //下一月
            $scope.customNext=function(){
                var cm= $scope.cmonth+1;
                if(cm>11){
                    $scope.cmonth=0;
                    $scope.year=$scope.year+1;
                }else{
                    $scope.cmonth=cm;
                }
                $scope.month =  $scope.months[$scope.cmonth];
                $scope.refreshDay();
            }
            //特殊日排房
          $scope.terraPolicy = function(day){
             if(day.status==2){
             	var month=$scope.cmonth+1
             	if(parseInt(month) < 10) {
              		var ymonth='0'+month;
              		
              	}else{
              		var ymonth=month
              	}
              	if(parseInt(day.day) < 10) {
              		var yday='0'+day.day;
              		
              	}else{
              		var yday=day.day
              	}
              	
             	var time=$scope.year+"-"+ymonth+"-"+yday
             	localStorageService.set("status",day.status);
             	localStorageService.set("rukuTime",time);
            	$location.path("/day-check");
             }else{
             	alert("无异常")
             }
          };

            $scope.cancel=function(){
                //ngDialog.closeAll();
            }
        }
    };
}]);

